﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using com.vs.General;
using com.vs.General.RF;
using com.vs.DataAccess.DO;
using com.vs.Task.BL;

namespace com.vs.RF.Web.task
{
    public partial class PickFromEntryHandling: RFHandlingPage
    {
        private List<TaskDetailDO> ltFromTaskDtl = null;
        private List<TaskDetailDO> ltToTaskDtl = null;
        private TaskDetailDO tskDtlDO = null;
        private TaskPreferenceDO prefDO = null;
        private int iConfirmQty = 0;
        private bool bAutoPutaway = false;
        private string stAction = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            stSourcePage = "task/PickFromEntry.aspx";
            stTargetPage = "task/PickFromEntry.aspx";
            go();
        }

        protected override void initData()
        {
            ltFromTaskDtl = (List<TaskDetailDO>)Session[RFSupport.FROM_TASK_DETAIL_DO_LIST];
            this.tskDtlDO = ltFromTaskDtl[0];
            this.prefDO = (TaskPreferenceDO)Session[RFSupport.TASK_PREFERENCE_DO];
            this.bAutoPutaway = StringManager.Equals(prefDO.AUTO_PUTAWAY, "Y");
        }

        protected override void parseRequest()
        {
            stAction = Request["hdAction"];
            if (StringManager.Equals(stAction, RFSupport.PICK))
                iConfirmQty = Convert.ToInt32(Request["tbQty"]);
        }


        protected override void procData()
        {
            if (StringManager.Equals(stAction, RFSupport.PICK))
                this.procConfirm();
            else if (StringManager.Equals(stAction, RFSupport.SKIP))
                this.procSkip();
            else if (StringManager.Equals(stAction, RFSupport.PUTAWAY))
                this.procPutaway();
            else if (StringManager.Equals(stAction, RFSupport.PASS))
                this.procPass();
        }

        protected void procConfirm()
        {
            TaskConfirmDS tcDS = null;
            TaskExecutionHelper tskMgr = null;

            try
            {
                tcDS = new TaskConfirmDS();
                tcDS.INTERNAL_TASK_LINE_NUM = this.tskDtlDO.INTERNAL_TASK_LINE_NUM;
                tcDS.ACTUAL_FROM_LOC = this.tskDtlDO.FROM_LOC;
                tcDS.ACTUAL_FROM_LPN = this.tskDtlDO.FROM_LPN;
                tcDS.CONFIRM_QTY = iConfirmQty;

                //如果源库位是记录LPN的，那么用户在pick之后也应该在身上记录LPN。这个LPN就是源库位的LPN
                //如果是pick into Shipping container的情况，lpn应该是用户输入的container号
                if (bAutoPutaway)
                {
                    tcDS.ACTUAL_TO_LOC = this.tskDtlDO.TO_LOC;
                    tcDS.ACTUAL_TO_LPN = this.tskDtlDO.FROM_LPN;
                }
                else
                {
                    tcDS.ACTUAL_TO_LOC = "-" + rfSess.User;
                    tcDS.ACTUAL_TO_LPN = this.tskDtlDO.FROM_LPN;
                }
                tcDS.CONFIRM_USER = rfSess.User;

                tskMgr = new TaskExecutionHelper();
                if (bAutoPutaway)
                    stError = tskMgr.ConfirmTask(rfSess, tcDS);
                else
                    stError = tskMgr.ConfirmFromTask(rfSess, tcDS);

                if (!StringManager.IsEmpty(stError))
                    return;

                //如果不是自动上架，添加到上架任务列表中
                if (!bAutoPutaway)
                {
                    ltToTaskDtl = (List<TaskDetailDO>)Session[RFSupport.TO_TASK_DETAIL_DO_LIST];
                    if (ltToTaskDtl == null)
                        ltToTaskDtl = new List<TaskDetailDO>(4);

                    this.tskDtlDO.TO_QTY = this.iConfirmQty;
                    ltToTaskDtl.Add(this.tskDtlDO);
                    Session[RFSupport.TO_TASK_DETAIL_DO_LIST] = ltToTaskDtl;
                }

                //完成拣货后从拣货任务列表移出，如果部分拣选，则更新TaskDetail列表
                if (iConfirmQty < this.tskDtlDO.FROM_QTY)
                    ltFromTaskDtl[0].FROM_QTY = tskDtlDO.FROM_QTY - iConfirmQty;
                else
                    this.ltFromTaskDtl.RemoveAt(0);


                //如果所有任务明细都拣货完成
                if (ltFromTaskDtl.Count == 0)
                {
                    if (bAutoPutaway)
                        stTargetPage = "task/TaskIDEntry.aspx";
                    else
                        stTargetPage = "task/PutToEntry.aspx";
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(rfSess, e);
                stError = e.Message;
            }
        }

        protected void procSkip()
        {
            try
            {
                if (ltFromTaskDtl.Count > 1)
                {
                    ltFromTaskDtl.RemoveAt(0);
                    ltFromTaskDtl.Add(tskDtlDO);
                    Session[RFSupport.FROM_TASK_DETAIL_DO_LIST] = ltFromTaskDtl;
                }
                else
                    stError = MessageManager.MSG_TSK_024;//没有其余任务明细了。
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(rfSess, e);
                stError = e.Message;
            }
        }

        protected void procPutaway()
        {
            stTargetPage = "task/PutToEntry.aspx";
        }

        protected void procPass()
        {
            Dictionary<string, object> dcValue = null;
            TaskHeaderDO tskHdrDO = null;

            try
            {
                //判断任务是否有被执行，如果没有，更新任务状态回OPEN
                if ((ltFromTaskDtl == null || ltFromTaskDtl.Count < 1) && (ltToTaskDtl == null || ltToTaskDtl.Count < 1)) 
                {
                    dcValue = new Dictionary<string, object>();
                    dcValue.Add("ASSIGNED_USER", null);
                    dcValue.Add("CONDITION", Constants.stOPEN);

                    tskHdrDO = new TaskHeaderDO();
                    tskHdrDO.INTERNAL_TASK_NUM = tskDtlDO.INTERNAL_TASK_NUM;
                    stError = tskDtlDO.Update(rfSess, dcValue);
                }

                if (StringManager.IsEmpty(stError))
                    stTargetPage = "task/TaskIDEntry.aspx";
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(rfSess, e);
                stError = e.Message;
            }
        }
    }
}
